package com.wish.g01.house.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g01.base.dto.PremisesQueryDto;
import com.wish.g01.base.entity.SysRegion;
import com.wish.g01.base.entity.TbBusinessCircle;
import com.wish.g01.base.entity.TbPremisesInfo;
import com.wish.g01.base.entity.TbSubway;
import com.wish.g01.base.service.ISysRegionService;
import com.wish.g01.base.service.ITbBusinessCircleService;
import com.wish.g01.base.service.ITbPremisesInfoService;
import com.wish.g01.base.service.ITbSubwayService;
import com.wish.g01.common.core.domain.R;
import com.wish.g01.common.core.web.controller.BaseController;
import com.wish.g01.common.core.web.domain.AjaxResult;
import com.wish.g01.common.core.web.page.TableDataInfo;
import com.wish.g01.base.entity.*;
import com.wish.g01.base.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 楼盘信息Controller
 * 
 * @author wish
 * @date 2022-05-24
 */
@RestController
@RequestMapping("/info")
public class PremisesInfoController extends BaseController
{
    @Resource
    private ITbPremisesInfoService premisesInfoService;

    @Resource
    private ISysRegionService regionService;

    @Resource
    private ITbBusinessCircleService businessCircleService;

    @Resource
    private ITbSubwayService subwayService;

    @Resource
    private ITbConfigPremisesPropertyService premisesPropertyService;

    @Resource
    private ITbRelationPremisesPropertyService relationPremisesPropertyService;

    @Autowired
    private ITbPremisesFacilityService premisesFacilityService;

    @Autowired
    private ITbAlbumService tbAlbumService;
    /**
     * 查询楼盘信息列表
     **/

//    @PreAuthorize(hasPermi = "system:info:list")
    @DS("slave")
    @GetMapping("/list")
    public TableDataInfo list(PremisesQueryDto premisesQueryDto) {
        startPage();
        logger.info("getProvince:{}",premisesQueryDto.getProvince());
        if (premisesQueryDto.getPremisesType()!=null || !premisesQueryDto.getName().equals("") ||premisesQueryDto.getProvince()!=null || premisesQueryDto.getCity()!=null||premisesQueryDto.getCounty()!=null){
            List<TbPremisesInfo> list = premisesInfoService.searchList(premisesQueryDto);
            return  getDataTable(list);
        }
        List<TbPremisesInfo> list = premisesInfoService.searchList(null);
        return  getDataTable(list);
    }

    /**
     * 获取物业类型
     * @return
     */
    @RequestMapping("/getPremisesTypeList")
    public List<Map<String,Object>> getPremisesTypeList(){
        List<Map<String,Object>> list = premisesInfoService.getPremisesTypeList();
        return list;
    }

    /**
     * 省
     */
    @GetMapping("/getsheng")
    public List<SysRegion> getsheng(){
        List<SysRegion> getsheng = regionService.getsheng();
        return getsheng;
    }

    /**
     * 市
     */
    @GetMapping("/getshi")
    public List<SysRegion> getshi(TbPremisesInfo tbPremisesInfo){

        return regionService.getshi(tbPremisesInfo.getProvince());
    }

    /**
     * 县
     */
    @GetMapping("/getxian")
    public List<SysRegion> getxian(TbPremisesInfo tbPremisesInfo){
        return regionService.getshi(tbPremisesInfo.getCity());
    }

    /**
     * 获取商圈
     */
    @GetMapping("/getarea")
    public List<TbBusinessCircle> getarea(TbPremisesInfo tbPremisesInfo){
        return businessCircleService.getarea(tbPremisesInfo.getCounty());
    }

    /**
     * 获取直辖市地铁
     */
    @GetMapping("getzhixiasubway")
    public List<TbSubway> getzhixiasubway(TbPremisesInfo tbPremisesInfo){
        return subwayService.getsubway(tbPremisesInfo.getProvince());
    }

    /**
     * 获取楼盘特色
     * @return
     */
    @GetMapping("getproperty")
    public List<TbBusinessCircle> getproperty(){
        return premisesPropertyService.getproperty();
    }


    /**
     * 保存
     */
    @DS("master")
    @RequestMapping("/save")
    public R save(@RequestBody TbPremisesInfo premisesInfo) {

        premisesInfoService.saveOrUpdate(premisesInfo);
        logger.info("添加楼盘成功");

        for (Integer property:premisesInfo.getPropertys()) {
            TbRelationPremisesProperty tbRelationPremisesProperty = new TbRelationPremisesProperty();
            tbRelationPremisesProperty.setPropertyId(property);
            tbRelationPremisesProperty.setPremisesId(premisesInfo.getId());
            relationPremisesPropertyService.insertTbRelationPremisesProperty(tbRelationPremisesProperty);
            logger.info("特色中间表添加成功:{}",tbRelationPremisesProperty.toString());
        }
        for (TbPremisesFacility tbPremisesFacility : premisesInfo.getJiaotonglist()) {
            tbPremisesFacility.setPremisesId(premisesInfo.getId());
            tbPremisesFacility.setType(1);
            premisesFacilityService.insertTbPremisesFacility(tbPremisesFacility);
            System.out.println("交通添加成功:"+tbPremisesFacility.toString());
        }
        for (TbPremisesFacility tbPremisesFacility : premisesInfo.getZhoubianlist()) {
            tbPremisesFacility.setPremisesId(premisesInfo.getId());
            tbPremisesFacility.setType(0);
            premisesFacilityService.insertTbPremisesFacility(tbPremisesFacility);
            System.out.println("周边添加成功:"+tbPremisesFacility.toString());
        }
        //添加图片信息 tb_album
        if (premisesInfo.getField1()!=""){
            TbAlbum tbAlbum = new TbAlbum();
            tbAlbum.setUrl(premisesInfo.getField1());
            tbAlbum.setPremisesId(premisesInfo.getId());
            tbAlbum.setAlbumType(1);
            tbAlbum.setName("file");
            tbAlbumService.insertTbAlbum(tbAlbum);

        }
        if (premisesInfo.getField2()!=""){
            TbAlbum tbAlbum = new TbAlbum();
            tbAlbum.setUrl(premisesInfo.getField2());
            tbAlbum.setPremisesId(premisesInfo.getId());
            tbAlbum.setAlbumType(2);
            tbAlbum.setName("file");
            tbAlbumService.insertTbAlbum(tbAlbum);
        }
        if (premisesInfo.getField3()!=""){
            TbAlbum tbAlbum = new TbAlbum();
            tbAlbum.setUrl(premisesInfo.getField3());
            tbAlbum.setPremisesId(premisesInfo.getId());
            tbAlbum.setAlbumType(3);
            tbAlbum.setName("file");
            tbAlbumService.insertTbAlbum(tbAlbum);
        }
        if (premisesInfo.getField4()!=""){
            TbAlbum tbAlbum = new TbAlbum();
            tbAlbum.setUrl(premisesInfo.getField4());
            tbAlbum.setPremisesId(premisesInfo.getId());
            tbAlbum.setAlbumType(4);
            tbAlbum.setName("file");
            tbAlbumService.insertTbAlbum(tbAlbum);
        }
        if (premisesInfo.getField5()!=""){
            TbAlbum tbAlbum = new TbAlbum();
            tbAlbum.setUrl(premisesInfo.getField5());
            tbAlbum.setPremisesId(premisesInfo.getId());
            tbAlbum.setAlbumType(5);
            tbAlbum.setName("file");
            tbAlbumService.insertTbAlbum(tbAlbum);
        }


        return R.ok();
    }

    /**
     * 详情 detail
     */
    @RequestMapping("/detail")
    public TableDataInfo detail() {
        logger.info("***********");
        startPage();

        return getDataTable(null);
    }


    /**
     * 回显楼盘信息
     * @param id
     * @return
     */
    @GetMapping("selectById")
    public AjaxResult selectById(Integer id){
        QueryWrapper<TbPremisesInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        TbPremisesInfo premisesInfo1 = premisesInfoService.getOne(wrapper);
        /*QueryWrapper<TbRelationPremisesProperty> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("premises_id",id);
        List<TbRelationPremisesProperty> list = relationPremisesPropertyService.list(wrapper1);
        List<Integer> collect = list.stream().map(r -> r.getPropertyId()).collect(Collectors.toList());
        logger.info("setPremisesPropertyIds:{}"+collect);
//        premisesInfo1.setPropertys(collect.get());

        String buildingType = premisesInfo1.getBuildingType();
        List<Integer> bulidInt = new ArrayList<>();
        bulidInt.add(Integer.parseInt(buildingType));
        logger.info("setBuildingTypes:{}"+bulidInt);
//        premisesInfo1.setBuildingTypes(bulidInt);*/

        return AjaxResult.success(premisesInfo1);
    }


    /**
     * 修改楼盘信息
     */
//    @PreAuthorize(hasPermi = "house:info:update")
    @PostMapping("update")
    public AjaxResult update(@RequestBody TbPremisesInfo premisesInfo){
        premisesInfoService.updateById(premisesInfo);
        return AjaxResult.success();
    }
}
